﻿Imports System.Data.OleDb
Imports System.Security.Cryptography
Imports System.Text

Public Class UserDAO
    Shared Function DangNhap(ByVal user As UsersDTO) As Boolean
        user.passwork = GetMd5Hash(user.passwork)
        Dim Sql As String = "Select * From Users Where PassWork='" + user.passwork + "'"
        Dim cn As OleDbConnection
        cn = DataProvider.OpenCn()
        Dim da As New OleDbDataAdapter(Sql, cn)
        Dim dt As New DataTable
        da.Fill(dt)
        cn.Close()
        If (dt.Rows.Count > 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Shared Sub UpdateMK(ByVal users As UsersDTO)
        users.passwork = GetMd5Hash(users.passwork)
        Dim Sql As String = "Update Users Set Passwork='" + users.passwork + "'"
        Dim cn As OleDbConnection
        cn = DataProvider.OpenCn()
        Dim cm As New OleDbCommand(Sql, cn)
        cm.ExecuteNonQuery()
        cn.Close()
    End Sub

    Shared Sub UpdateGD(ByVal gid As UsersDTO)
        Dim Sql As String = "Update Users Set GiaoDien='" + gid.giaoDien + "'"
        Dim cn As OleDbConnection
        cn = DataProvider.OpenCn()
        Dim cm As New OleDbCommand(Sql, cn)
        cm.ExecuteNonQuery()
        cn.Close()
    End Sub

    Shared Function LoadGD() As DataTable
        Dim Sql As String = "Select GiaoDien From Users"
        Dim cn As OleDbConnection
        cn = DataProvider.OpenCn()
        Dim da As New OleDbDataAdapter(Sql, cn)
        Dim dt As New DataTable()
        da.Fill(dt)
        Return dt
    End Function


    Shared Function GetMd5Hash(ByVal input As String) As String
        Dim md5Hash As New MD5CryptoServiceProvider()
        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()
    End Function 'GetMd5Hash
End Class
